import QtQuick 1.0
import com.meego 1.0
import "Component"
import "CommomUtil.js" as Util
import "const.js" as Const
import "FavoritesPage.js" as Favorites
import "database.js" as DatabaseUtil
import "Modulelib.js" as ModuleUtil
import "Forum.js" as ForumUtil


/**
  * 收藏夹页面
  * shenjia.caosj 2011-08-14
  */

Page {
    id: favorites_page

    // 删除收藏夹Id, 车型和论坛都可以用
    property string favoriteIdForDel: "0"

    property string favoriteTypeForDel : ""

    BusyLoading {
        id : favorite_load
        anchors.fill: favorite_list_rect
        visible: true
    }

    ConfirmDialog{
        id: favorites_dialog
        diaContent: favorites_page.favoriteTypeForDel === Const.FavoriteType.Car ? "您将把该车型从收藏夹中删除?" : "您确定要删除该论坛收藏吗?"
        onAccepted: {
            DatabaseUtil.removeFavorite(favorites_page.favoriteIdForDel , favorites_page.favoriteTypeForDel) ;
            reloadFavorites() ;
        }
    }


    ConfirmDialog{
        id: deleteAll_dialog
        diaContent: "您确定要清空收藏夹吗?"
        onAccepted: {
            DatabaseUtil.clearFavorites(Const.FavoriteType.Car) ;
            DatabaseUtil.clearFavorites(Const.FavoriteType.Forum) ;
            reloadFavorites() ;
        }
    }

    ToolBarLayout {
        id: favoriteTools
        visible: true
        ToolIcon { iconId: "toolbar-back"; onClicked: pageStack.pop();}
        ToolIcon { iconId: "toolbar-delete"; onClicked: deleteAll_dialog.open();}
    }

    tools: favoriteTools



    orientationLock : PageOrientation.LockPortrait


    //fav type car / forum
    property string favorite_type: Const.FavoriteType.Car

    TitleBar {
        id: favorite_title
        anchors.top: favorites_page.top; anchors.left: favorites_page.left
        width: favorites_page.width; height: 71
        titleName: "收藏"
    }

    Rectangle{
        id: fav_toggle_rect
        color: Const.Color.PageBack
        anchors.top: favorite_title.bottom
        width: parent.width; height: 83
        z: 10
        ButtonRow{
        id: fav_toggle
        anchors.centerIn: parent
        width: parent.width - 30; height: 60

        platformStyle: CustomButtonStyle { }

        Button{
            text: "车 型";
            onClicked: {
                if(favorites_page.favorite_type != Const.FavoriteType.Car){
                    favorites_page.favorite_type = Const.FavoriteType.Car ;
                    reloadFavorites() ;
                }
            }
        }
        Button{
            text: "论 坛";
            onClicked: {
                if(favorites_page.favorite_type != Const.FavoriteType.Forum){
                    favorites_page.favorite_type = Const.FavoriteType.Forum ;
                    reloadFavorites() ;
                }
            }
        }
    }
    }
    Rectangle {
        id :favorite_list_rect
        width: parent.width; height: 623
        anchors.top: fav_toggle_rect.bottom
        anchors.left: favorites_page.left

        ListModel{

           id:favorite_list_model
        }



       //列表内容
       ListView {
            id: favorite_list
            model: favorite_list_model
            anchors.fill: parent
            delegate: Item {
                id:favorite_list_item
                width: 480; height: 120
                //background
                Rectangle {
                    id: favorite_list_item_back
                    anchors.fill: parent
                    border{color: "#c0c0c0"; width: 1}
                    gradient: Gradient {
                        GradientStop { position: 0.0; color: "white" }
                        GradientStop { position: 0.2; color: "#c0c5c6" }
                        GradientStop { position: 0.6; color: "#c0c5c6" }
                        GradientStop { position: 1.0; color: "white" }
                    }
                    Row{
                        spacing: 5
                        Item{
                            width:1; height: 120
                        }

                        Column{
                            Item{
                                width: 130; height: 10
                            }

                            Rectangle{
                                id: image_rect
                                color: "#c0c0c0"
                                width: 130; height: 100
                                Image{
                                    id: favorite_list_item_logo
                                    width: 120; height: 90
                                    anchors.centerIn: parent
                                    source: favLogo
                                }
                            }
                            Item{
                                width: 130; height: 10
                            }
                        }

                        Column{
                            spacing: 5
                            Item{
                                width: 330; height: 1
                            }
                            //title name
                            NormalText{
                                id :favorite_list_item_name
                                text: favName
                                width: 330; height: 40
                                color : Const.Color.darkBlue
                                font{bold: true; pointSize: 17}
                                horizontalAlignment: Text.AlignLeft
                                verticalAlignment: Text.AlignVCenter

                            }

                            Row{
                                spacing: 5
                            //指导价标题
                            NormalText{
                                id :favorite_list_item_price_desc
                                horizontalAlignment: Text.AlignLeft
                                verticalAlignment: Text.AlignVCenter
                                text: favorite_type==Const.FavoriteType.Car ? "厂商指导价:" : ""
                                color : Const.Color.black
                                font{bold: true; pointSize: 15}
                                width: 120; height: 25
                            }

                            // 价格
                            NormalText{
                                id :favorite_list_item_price
                                text: favorite_type==Const.FavoriteType.Car ? carGuidePrice : ""
                                color : "red"
                                horizontalAlignment: Text.AlignLeft
                                verticalAlignment: Text.AlignVCenter
                                font{bold: true; pointSize: 16}
                                width: 210; height: 25
                            }
                            }

                            //车系  或   版主
                            NormalText{
                                id :favorite_list_item_series_admins
                                text: favorite_type==Const.FavoriteType.Car ? "车系:" + carSeries : "版主:" + forumAdmins
                                color : Const.Color.black
                                horizontalAlignment: Text.AlignLeft
                                verticalAlignment: Text.AlignVCenter
                                font{bold: true; pointSize: 15}
                                width: 330; height: 25
                            }
                        }
                    }

                    MouseArea{
                        id: itemMouseArea
                        anchors.fill: parent
                        onPressAndHold:{
                            favorites_page.favoriteIdForDel = favId;
                            favorites_page.favoriteTypeForDel = favType;
                            favorites_dialog.open();
                            console.log("hold is going");
                        }
                        onClicked: {
                            if(favType === Const.FavoriteType.Car){ // 打开车型收藏页面
                                ModuleUtil.current_series_id = favId;
                                ModuleUtil.current_series_name = favName;
                                mainPage.pageStack.push(Qt.resolvedUrl("ModuleInfoPage.qml"));
                            }else{
                                ForumUtil.currentForumId = favId;
                            }
                        }

                    }

                    states: [
                        State {
                            name: 'pressed'; when: itemMouseArea.pressed
                            PropertyChanges { target: favorite_item_rect; focus: true; opacity: 0.5}
                        }
                    ]


                }

                Rectangle{
                    id: favorite_item_rect
                    anchors.fill: parent
                    color: "#000"
                    opacity: 0
                }

            }
            Component.onCompleted: {
                reloadFavorites() ;
            }

        }

    }


    /**
      * 刷新（重新载入）收藏
      */
    function reloadFavorites(){

        favorite_load.loading() ;


        var favorites = DatabaseUtil.getAllFavorites(favorite_type) ;
        favorite_list_model.clear() ;
        for(var i=0 ;i<favorites.length;i++){
            var favorite_item_tmp = favorites[i] ;
            //遍历加到列表中
            favorite_list_model.append({
                     sequence : favorite_item_tmp.sequence ,
                     favId :favorite_item_tmp.favoriteId ,
                     favName : favorite_item_tmp.name ,
                     favLogo : favorite_item_tmp.logo ,
                     carSeries :favorite_item_tmp.carSeries ,
                     carGuidePrice : favorite_item_tmp.carGuidePrice ,
                     forumAdmins : favorite_item_tmp.forumAdmins ,
                     favType : favorite_item_tmp.favoriteType ,
                     favIndex : i
                 }) ;
        }

        favorite_load.finish() ;
    }
}
